﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EM.Components.Entities;
using System.Threading.Tasks;

namespace EM.Components.DataAccess
{
    public class CampaignDataAccess:BaseDataAccess,IEntityDataService<EM_Campaigns>
    {
        public CampaignDataAccess() : base() 
        {
        }

        public EM_Campaigns Get(string name)
        {
            var cc = from c in base.Ctx.EM_Campaigns
                      where c.CampaignName.ToLower() == name.ToLower()
                      select c;
            EM_Campaigns result = null;
            try
            {
                result = cc.FirstOrDefault();
            }
            catch 
            {
                //Log exception
            }
            return result;
        }

        public EM_Campaigns Get(int entityId)
        {
            ValidateEntityId(entityId);
            var entity = (from c in base.Ctx.EM_Campaigns
                     where c.CampaignID == entityId
                     select c).FirstOrDefault<EM_Campaigns>();
            return entity;
        }

        public EM_Campaigns Get(Guid guid) 
        {
            var cc = from c in base.Ctx.EM_Campaigns
                     where c.Guid == guid
                     select c;
            EM_Campaigns result=null;

            try
            {
                result = cc.First<EM_Campaigns>();
            }
            catch (Exception ex) 
            { 
                // log exception
            } 
            return result;
        }

        public EM_Campaigns Update(EM_Campaigns entity)
        {
            ValidateEntityId(entity.CampaignID);
            ValidateNull<EM_Campaigns>(entity);

            var existing = Get(entity.CampaignID);
            existing.CampaignName = entity.CampaignName;
            existing.CampaignDesc = entity.CampaignDesc;
            existing.StartDate = entity.StartDate;
            existing.EndDate = entity.EndDate;
            existing.Submitted = entity.Submitted;
            existing.Approved = entity.Approved;
            existing.Owner = entity.Owner;
            existing.ApprovedBy = entity.ApprovedBy;
            existing.ApprovedDate = entity.ApprovedDate;

            base.Ctx.SaveChanges();
            return entity;
        }

        public EM_Campaigns Add(EM_Campaigns entity)
        {
            ValidateNull<EM_Campaigns>(entity);
            base.Ctx.EM_Campaigns.AddObject(entity);
            base.Ctx.SaveChanges();

            return entity;
        }

        public void Delete(EM_Campaigns entity)
        {
            ValidateEntityId(entity.CampaignID);
            ValidateNull<EM_Campaigns>(entity);

            Delete(entity.CampaignID);

        }

        public void Delete(int entityId)
        {
            ValidateEntityId(entityId);
            var entity = Get(entityId);

            if (entity == null)
            {
                throw new ArgumentException("Entity doesn't exist", entity.GetType().ToString() + "entity Id" + entity.CampaignID);
            }
            else 
            {
                base.Ctx.EM_Campaigns.DeleteObject(entity);
                base.Ctx.SaveChanges();
            }
        }

         
        public void CreateCampaignInstances(int emailInstanceID) 
        {
            Ctx.EM_CampaignInstances_INSERT(emailInstanceID);
        }

        public List<EM_Campaigns> GetAll()
        {
            var all=from c in base.Ctx.EM_Campaigns
                    select c;

            return all.ToList<EM_Campaigns>();
        }
    }
}
